define(function(require) {

    require('./white-user-tpl');
    require('./user-list-tpl');

    var userTpl = juicer.templates['white-user.juicer.html'];
    var listTpl = juicer.templates['user-list.juicer.html'];

    var order = {};


    var User = {

        shopId : null,

        init : function() {
            this.bindEvent();
        },
        showModal : function() {
            var self = this;

            CRM.showModal(userTpl({
                "shopId" : self.shopId
            }), $('.container-fluid'));


            //$('.container-fluid').append(userTpl({"shopId":self.shopId||''}));
            //$('#wihite_list_dialog').modal('show');
            //$('#wihite_list_dialog').on('hidden.bs.modal', function (e) {
            //    $('#wihite_list_dialog').remove();
            //});
        },
        getUserByShopAndMobile : function(shopId) {
            var self = this;
            CRM.request({
                "type": 'GET',
                "url": CRM.getOuterUrl('/member/getUserByShopAndMobile.do'),
                "dataType": "json",
                "data": {
                    "shopId" : self.shopId,
                    "mobile" : $('#search_phone').val()
                },
                "success" : function(req) {
                    var entry = req.entry;
                    if(req.status) {
                        $('.white-user-list').html(listTpl(entry));
                    }else{
                        CRM.showDialog(req.message)
                    }
                }
            });
        },

        addUser : function() {
            var self = this;
            CRM.request({
                "type": 'POST',
                "url": CRM.getOuterUrl('/member/addUserShop.do'),
                "dataType": "json",
                "data": JSON.stringify({
                    "shopId" : self.shopId,
                    "mobile" : $('#tel_phone').val()
                }),
                "success" : function(req) {
                    if (req.responseCode == 1 && req.status) {
                        CRM.showDialog(req.message);
                    }else{
                        CRM.showDialog(req.message);
                    }
                }
            });
        },

        deleteUser : function(node) {
            var self = this;
            CRM.request({
                "type": 'POST',
                "url": CRM.getOuterUrl('/member/deleteUserShop.do'),
                "dataType": "json",
                "data": JSON.stringify({
                    "shopId" : self.shopId,
                    "mobile" : node.attr('data-mobile')
                }),
                "success" : function(req) {
                    if (req.status) {
                        CRM.showDialog(req.message);
                        node.parents('div.white-user-list').html('');
                    }else{
                        CRM.showDialog(req.message);
                    }
                }
            });
        },

        bindEvent : function() {
            var self = this;
            $(document).on('click', '.J_White', function(){
                self.shopId = $(this).attr('data-id');
                self.showModal();
            });
            $(document).on('click', '#J_AddUserShop', function() {
                //alert(self.shopId);
                self.addUser();
            });
            $(document).on('click', '#J_SearchUserShop', function() {
                //alert(self.shopId);
                self.getUserByShopAndMobile();
            });
            $(document).on('click', '#J_DeleteUserShop', function(){
                self.deleteUser($(this));
            });

        }
    };

    return User;
});
